#!/usr/bin/python
#
# Copyright (c) 2008, Mathias Weber
# Copyright (c) 2008, Thomas Stauffer
# All righstage reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFIstage; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# $Id: example-vfs-benchmark.py 78 2008-09-21 21:45:24Z tymayos $

import time
import sys
import random
import os

"""
TODO add explanation how to re-test
"""

"""
multify -czf 000.bin 001.bin 002.bin 003.bin 004.bin 005.bin 006.bin 007.bin 008.bin 009.bin
"""

"""
Size

Raw = 100 MB, Multify = 83 MB, ZIP = 83 MB, 7Zip = 4.3 MB

Speed (after computer reboot):

First Raw = 6.2 s, Second Raw = 0.9 s, Third Raw = 0.9 s
First Multify = 6.9 s, Second Multify = 2.1 s, Third Multify = 2.1 s

parts = []
for i in range(128):
    part = ""
    for j in range(10240):
        part += chr(random.randint(32, 126))
    parts += [ part ]

for i in range(10):
    filename = "%03i.bin" %i
    print filename
    f = open(filename, "wb")
    for j in range(1024):
        f.write(random.choice(parts))
    f.close()

sys.exit(1)
"""

"""

Size

Raw = 100 MB, Multify = 32 MB, ZIP = 32 MB, 7Zip = 0.6 MB

Speed (after computer reboot)

First VFS = 4.2 s, Second VFS = 0.9 s, Third VFS = 0.9 s
First Multify = 3.6 s, Second Multify = 1.45 s, Second Multify = 1.45 s
Python First = 4.06 s, Second Python = 0.15 s, Third Python = 0.15 s

parts = []
for i in range(128):
    part = ""
    for j in range(256):
        part += chr(random.randint(32, 126))
    parts += [ part ]

for i in range(10):
    filename = "%03i.bin" %i
    print filename
    f = open(filename, "wb")
    for j in range(1024 * 40):
        f.write(random.choice(parts))
    f.close()

sys.exit(1)
"""

"""
from pandac.PandaModules import VirtualFileSystem, Filename

vfs = VirtualFileSystem.getGlobalPtr()
#vfs.mount(Filename("example-vfs-benchmark"), ".", VirtualFileSystem.MFReadOnly)

t1 = time.time()
"""

"""
for filename in os.listdir("."):
    if filename.endswith("bin"):
        print filename
        s = open(filename, "rb").read()
        print len(s)
"""

"""
l = vfs.getFile(Filename(".")).scanDirectory()
for i in range(l.getNumFiles()):
    f = l.getFile(i)
    filename = str(f.getFilename())
    if filename.endswith("txt"):
        print filename
        s = f.readFile(True)
        print s
    elif filename.endswith("bin"):
        print filename
        s = f.readFile(True)
        print len(s)
"""

"""
t2 = time.time()

print t2 - t1

sys.exit(1)
"""
